package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.weixin.paleituo.BuildConfig;
import com.weixin.paleituo.R;
import com.weixin.paleituo.base.BasePersonActivity;
import com.weixin.paleituo.bean.AboutUsBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * 关于我们
 *
 * @author fengzhen
 * @version v1.0, 2018/4/14
 */
public class AboutUsActivity extends BasePersonActivity {

    @BindView(R.id.tv_version_activity_about_us)
    TextView mTvVersionActivityAboutUs;
    @BindView(R.id.tv_website_activity_about_us)
    TextView mTvWebsiteActivityAboutUs;
    @BindView(R.id.tv_phone_activity_about_us)
    TextView mTvPhoneActivityAboutUs;
    @BindView(R.id.tv_info_activity_about_us)
    TextView mTvInfoActivityAboutUs;

    private LoadingPagerHead mLoadingPagerHead;

    public static Intent call2AboutUsActivity(Context context) {
        Intent intent = new Intent(context, AboutUsActivity.class);
        return intent;
    }

    @Override
    protected View getContentView(Context context) {
        mLoadingPagerHead = new LoadingPagerHead(this) {
            @Override
            protected void close() {
                onBackPressed();

            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(AboutUsActivity.this, R.layout.activity_about_us);
            }

            @Override
            protected void reLoading() {
                getAboutUs();
            }
        };
        return mLoadingPagerHead;
    }

    @Override
    protected void initData() {
        super.initData();
        mTvTitle.setText("关于我们");
        getAboutUs();
    }

    private void getAboutUs() {
        mLoadingPagerHead.showPagerView(Contans.STATE_LOADING);
        Observable<StatusCode<List<AboutUsBean>>> observable = ApiUtils.getApi().getAboutUs(Contans.defaultToken);
        HttpUtil.getInstance().toSubscribe(observable,
                new ProgressSubscriber<List<AboutUsBean>>(this) {
                    @Override
                    protected void _onNext(StatusCode<List<AboutUsBean>> statusCode) {
                        List<AboutUsBean> data = statusCode.getData();
                        mTvVersionActivityAboutUs.setText(String.format(getString(R.string.string_us_version), BuildConfig.VERSION_NAME));
                        if (data != null) {
                            mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                            mTvInfoActivityAboutUs.setText(data.get(0).getPresentation());
                            mTvPhoneActivityAboutUs.setText(
                                    String.format(getString(R.string.string_us_phone), data.get(0).getServicetel())
                            );
                            if (TextUtils.isEmpty(data.get(0).getOffcialws())) {
                                mTvWebsiteActivityAboutUs.setVisibility(View.INVISIBLE);
                            } else {
                                mTvWebsiteActivityAboutUs.setVisibility(View.VISIBLE);
                                mTvWebsiteActivityAboutUs.setText(
                                        String.format(getString(R.string.string_us_website), data.get(0).getOffcialws())
                                );
                            }
                        } else {
                            mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);
                    }
                }, "", lifecycleSubject, false, true);
    }
}
